package com.zd.application.web;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zd.application.common.C;
import com.zd.application.entity.User;
import com.zd.application.service.AuthoritiesService;
import com.zd.application.service.ManageUtil;
import com.zd.application.service.UserHandler;
import com.zd.core.dao.UserService;
import com.zd.core.dao.entity.Page;

@Controller
@RequestMapping(DataController.PATH)
public class DataController {
	public final static String PATH = "/data";

	@RequestMapping
	public ModelAndView list(Double lat, Double lng) {
		ModelAndView mv = new ModelAndView();
		return mv;
	}
	
	@Resource
	private AuthoritiesService authoritiesService;
	
	@Resource
	private UserService userService;
		
	@RequestMapping("/test")
	public ModelAndView test(HttpServletRequest request) throws SQLException {
		/*WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		authoritiesService = context.getBean(AuthoritiesService.class);
		authoritiesService.Save();*/
		User user = new User();
		user.setUsername("admin2");
		user.setPassword("123456");
		user.setEnabled(true);
		userService.save(user);
		Page<User> page = new Page<User>();		
		List<User> users = userService.select().findListBean("select * from t_user");
		System.out.println(Arrays.toString(users.toArray()));
		return null;
	}
	
	/**
	 * 发送自己的状态
	 * @param session
	 * @param lat
	 * @param lng
	 */
	@RequestMapping("/send")
	public ModelAndView send(HttpSession session, Double lat, Double lng) {
		//获取自己
		UserHandler userHandler = (UserHandler)session.getAttribute("userHandler");
		if (!C.isEmpty(lat) && !C.isEmpty(lng)) {
			User user = userHandler.getMyself();
			user.setLat(Double.valueOf(lat));
			user.setLng(Double.valueOf(lng));
			userHandler.change();
		}
		//获取改变状态(坐标)的用户
		User[] others = userHandler.getChanges();
		ModelAndView mv = new ModelAndView();
		mv.addObject(others);
		JSONArray j = JSONArray.fromObject(others);
		System.out.println(session.getId() + " " + j);
		return mv;
	}
	
	@RequestMapping("/login")
	public ModelAndView login(HttpSession session, User user) {
		UserHandler userHandler = (UserHandler) session.getAttribute("userHandler");
		if(userHandler != null)
			return new ModelAndView();
		userHandler = new UserHandler();
		User myself = userHandler.getMyself();
		//使用sessionId作为用户id
		//myself.setUid(session.getId());**
		//设置用户名
		myself.setUsername(user.getUsername());
		ServletContext context = session.getServletContext();
		Hashtable<String, UserHandler> userHandlers = (Hashtable<String, UserHandler>) context.getAttribute("userHandlers");			
		for (UserHandler userHandler2 : userHandlers.values()) {
			//将别人注册为自己的观察者
			userHandler.addObserver(userHandler2);
			//将自己注册为其他用户的观察者
			userHandler2.addObserver(userHandler);
			//登录时，获取其他所有人的信息
			userHandler.update(null, userHandler2.getMyself());
		}
		//将自己放到用户操作集合中
		//userHandlers.put(myself.getUid(), userHandler);**
		session.setAttribute("userHandler", userHandler);
		
		ModelAndView mv = new ModelAndView();
		//TODO:返回登录成功标志
		return mv;
	}
	
	/**
	 * 退出
	 * @param session
	 * @param user
	 * @return
	 */
	@RequestMapping("/logoff")
	public ModelAndView logoff(HttpSession session, User user) {
		UserHandler userHandler = (UserHandler) session.getAttribute("userHandler");
		ServletContext app = session.getServletContext();
		Hashtable<String, UserHandler> userHandlers =  (Hashtable<String, UserHandler>) app.getAttribute("userHandlers");
		ManageUtil.kick(userHandler, userHandlers);
		ModelAndView mv = new ModelAndView();
		//TODO:返回退出成功标志
		return mv;
	}
	
	/**
	 * 获取所有在线用户
	 * @param session
	 * @return
	 */
	@RequestMapping("/online")
	public ModelAndView online(HttpSession session) {
		ServletContext context = session.getServletContext();
		Hashtable<String, UserHandler> userHandlers = (Hashtable<String, UserHandler>) context.getAttribute("userHandlers");
		ModelAndView mv = new ModelAndView();		
		mv.addObject(userHandlers);
		return mv;
	}
	
//	@RequestMapping("/kickall")
//	public void kickall(HttpSession session) {
//		ServletContext context = session.getServletContext();		
//		Hashtable<String, UserHandler> userHandlers = (Hashtable<String, UserHandler>) context.getAttribute("userHandlers");
//		userHandlers.clear();
//	}
}
